/*
 * @Author: 林文琛
 * @Date: 2022-01-05 16:04:55
 * @LastEditors: 林文琛
 * @LastEditTime: 2022-06-30 13:45:13
 * @Description: 通用工具类
 */

export class Common {

    /**
     * @name: toObj
     * @Date: 2022-05-10 10:38:21
     * @description: 根据属性和值，将对象数组转换成新的对象
     * @param {object} list 用于转换的对象数组
     * @param {string} label 用于转换的属性
     * @param {string} value 用于转换的值
     * @return {object} 新的对象
     */
    public static toObj = (list: object[], label: string, value: string): object => {
        let obj: object = {}
        list.forEach(e => {
            obj[e[label]] = e[value]
        })
        return obj
    }

    /**
     * @name: toObjSelf
     * @Date: 2022-05-10 10:39:48
     * @description: 根据属性，将对象数组转换成包含自己的新对象
     * @param {object} list 用于转换的对象数组
     * @param {string} label 用于转换的属性
     * @return {object} 新的对象
     */
    public static toObjSelf = (list: object[], label: string): object => {
        let obj: object = {}
        list.forEach(e => {
            obj[e[label]] = e
        })
        return obj
    }

    /**
     * @name: isEmObj
     * @Date: 2022-05-10 10:40:58
     * @description: 判断对象是否为空
     * @param {object} obj 判断的对象
     * @return {boolean} 是否为空
     */
    public static isEmObj = (obj: object): boolean => {
        if (Object.keys(obj).length === 0) {
            return true
        }
        return false
    }

    /**
     * @name: findTreeItem
     * @Date: 2022-05-10 10:41:32
     * @description: 单根树查找值
     * @param {object} tree 树
     * @param {string} key 标识
     * @param {number} value 查找值
     * @param {string} childName 子集名称
     * @return {object | undefined} 查找结果
     */
    public static findTreeItem = (tree: object, key: string, value: number | string, childName: string = "children"): object | undefined => {
        if (value == tree[key]) {
            return tree;
        }
        if (tree[childName]) {
            for (let c of tree[childName]) {
                let res = this.findTreeItem(c, key, value, childName);
                if (res) {
                    return res;
                }
            }
        }
        return undefined;
    }

    /**
     * @name: findTreesItem
     * @Date: 2022-05-10 10:44:11
     * @description: 多根树查找值
     * @param {object} trees 树
     * @param {string} key 标识
     * @param {number} value 查找值
     * @param {string} childName 子集名称
     * @return {object | undefined} 查找结果
     */
    public static findTreesItem = (trees: object[], key: string, value: number | string, childName: string = "children"): object | undefined => {
        for (let tree of trees) {
            let res = this.findTreeItem(tree, key, value, childName);
            if (res) {
                return res;
            }
        }
        return undefined;
    }

    /**
     * 获取时间，格式：20220505104102
     * @returns {string} 当前时间
     */
    public static getNowTime = (): string => {
        var date = new Date();
        //年 getFullYear()：四位数字返回年份
        var year = date.getFullYear();  //getFullYear()代替getYear()
        //月 getMonth()：0 ~ 11
        var month = date.getMonth() + 1;
        //日 getDate()：(1 ~ 31)
        var day = date.getDate();
        //时 getHours()：(0 ~ 23)
        var hour = date.getHours();
        //分 getMinutes()： (0 ~ 59)
        var minute = date.getMinutes();
        //秒 getSeconds()：(0 ~ 59)
        var second = date.getSeconds();
        let month1 = Number(month) < 10 ? `0${month}` : month
        let day1 = Number(day) < 10 ? `0${day}` : day
        let hour1 = Number(hour) < 10 ? `0${hour}` : hour
        let minute1 = Number(minute) < 10 ? `0${minute}` : minute
        let second1 = Number(second) < 10 ? `0${second}` : second
        return '' + year + month1 + day1 + hour1 + minute1 + second1
    }

    /**
     * @name: getAssetsImage
     * @Date: 2022-05-10 10:33:38
     * @description: 获取图片
     * @param {string} name 图片路径
     * @return {string} 图片
     */
    public static getAssetsImage = (name: string): string => {
        return new URL(`/src/assets/img/${name}`, import.meta.url).href;
    }

    /**
     * 获取文件大小
     * @param size
     * @returns
     */
    public static getFileSize = (size: number): string => {
        if (!size) return "";
        let num = 1024;
        if (size < num) {
            return `${size}B`
        }
        if (size < Math.pow(num, 2))
            return (size / num).toFixed(2) + "KB";
        if (size < Math.pow(num, 3))
            return (size / Math.pow(num, 2)).toFixed(2) + "MB";
        if (size < Math.pow(num, 4))
            return (size / Math.pow(num, 3)).toFixed(2) + "G";
        return (size / Math.pow(num, 4)).toFixed(2) + "T";
    }

    /**
     * 导出成excel
     * @param data 接口返回的数据
     * @param filename 导出文件名
     */
    public static exportExcel = (data: any, filename: string = '文件') => {
        let elink = document.createElement('a');
        elink.download = filename;

        let blob = new Blob([data]);
        const downloadUrl = URL.createObjectURL(blob);
        elink.href = downloadUrl
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
        URL.revokeObjectURL(downloadUrl) // 释放资源
    }

    public static addUnit = (value?: string | number, defaultUnit = 'px') => {
        if (!value) return ''
        if (typeof value === 'string') {
            return value
        } else if (typeof value === 'number') {
            return `${value}${defaultUnit}`
        }
    }

    public static getDate = () => {
        const dateObj = new Date()
        const date = `${dateObj.getFullYear().toString().padStart(2, '0')}.${(dateObj.getMonth() + 1).toString().padStart(2, '0')}.${dateObj.getDate().toString().padStart(2, '0')}`
        const time = `${dateObj.getHours().toString().padStart(2, '0')}:${dateObj.getMinutes().toString().padStart(2, '0')}:${dateObj.getSeconds().toString().padStart(2, '0')}`
        const week= {
            0: '星期天',
            1: '星期一',
            2: '星期二',
            3: '星期三',
            4: '星期四',
            5: '星期五',
            6: '星期六'
        }
        const day = week[dateObj.getDay()]
        return {
            date,
            time,
            day
        }
    }
}
